package com.dolojia.javafxboot.ctrl;

import com.dolojia.javafxboot.util.ViewLoader;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 主界面控制器
 *
 * @author westinyang
 * @date 2019/4/23 2:01
 */
@Slf4j
@FXMLController
public class MainCtrl implements Initializable {
    // 内容区域的ScrollPane
    @FXML
    private ScrollPane body;
    @FXML
    private Label title;
    // 左侧菜单栏
    @FXML
    private VBox drawer;
    // 主内容区域
    @FXML
    private VBox content;
    // 记录原始宽度
    private double originalDrawerWidth;
    private boolean isDrawerOpen = true;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        log.info("initialize: {}", location.getPath());
        // 默认加载仪表板视图
        ViewLoader.loadFXMLView(body, title, "/fxml/dashboard.fxml", "Dashboard");
        // 保存原始宽度
        originalDrawerWidth = drawer.getPrefWidth();
        // 设置初始状态
        drawer.setPrefWidth(originalDrawerWidth);
        drawer.setVisible(true);
        drawer.setManaged(true);

        // 可选：添加平滑过渡动画
        drawer.widthProperty().addListener((obs, oldWidth, newWidth) -> {
            content.requestLayout();
            body.requestLayout();
        });
        // 在initialize方法中添加窗口大小监听
        body.sceneProperty().addListener((obs, oldScene, newScene) -> {
            if (newScene != null) {
                newScene.widthProperty().addListener((obs2, oldWidth, newWidth) -> {
                    if (newWidth.doubleValue() < 400 && isDrawerOpen) {
                        altLayout(null); // 小屏幕自动隐藏菜单
                    }
                });
            }
        });
    }


    public void dashboard() {
        log.info("dashboard");
        // 默认加载仪表板视图
        ViewLoader.loadFXMLView(body, title, "/fxml/dashboard.fxml", "Dashboard");

    }

    public void jfxTextField() {
        log.info("JavaFxXmlToObjectCode");
        ViewLoader.loadFXMLView(body, title, "/fxml/JavaFxXmlToObjectCode.fxml", "JavaFxXmlToObjectCode");
    }

    public void about() {
        log.info("about");
        ViewLoader.loadFXMLView(body, title, "/fxml/profile.fxml", "About");
    }


    public void webview() {
        log.info("webview baidu");
        ViewLoader.loadWebView(body, title, "https://www.baidu.com", "WebView - Baidu");
    }

    public void webview2() {
        log.info("webview bing");
        ViewLoader.loadWebView(body, title, "https://www.bing.com", "WebView - Bing");
    }

    public void altLayout(MouseEvent mouseEvent) {
        log.info("altLayout");
        isDrawerOpen = !isDrawerOpen;

        if (isDrawerOpen) {
            // 显示菜单
            drawer.setPrefWidth(originalDrawerWidth);
            drawer.setVisible(true);
            drawer.setManaged(true);
        } else {
            // 隐藏菜单
            drawer.setPrefWidth(0);
            drawer.setVisible(false);
            drawer.setManaged(false);
        }
        // 强制重新布局
        content.requestLayout();
        body.requestLayout();
    }


}
